$(function () {
    jeDate("#indate", {
        format: "YYYY年MM月DD日",
        isTime: false,
        minDate: "2014-09-19 00:00:00",
        zIndex:'99999999'
    });
    var E = window.wangEditor
    var editor2 = new E('#text')
    editor2.create();
 //文章分类初始化渲染
    $.ajax({
        url: BigNew.category_list,
        success(backData) {
            let Html = template('categoryId', backData);
            $('[name=categoryId]').html(Html);
        }
    });
    // 图片预览功能
    $("#inputCover").change(function () {
        let file = this.files[0];
        let url = URL.createObjectURL(file);
        $(this).prev('img').attr('src', url);
    });
    //文章内容初始化
    let id = window.location.search.split('=')[1];
    $.ajax({
        url: BigNew.article_search,
        data: {
            id
        },
        success(backData) {
            if (backData.code == 200) {
                $('#form #inputTitle').val(backData.data.title);
                $('#form .article_cover').attr('src', backData.data.cover);
                $('#selCategory').val(backData.data.categoryId);
                $('#indate').val(backData.data.date);
                editor2.txt.html(backData.data.content);
            }
           
        }
    });

    //修改功能
    $('#form .btn-edit').click(function (e) {
        e.preventDefault();
        // data = new FormData($('#form')[0]);
        // let id = $(this).parent().attr('data-id');
        let state = '已发布';
        // console.log();
        articleEdit(state, '发布成功');
    });
      //存草稿功能
    $('#form .btn-draft').click(function (e) {
        e.preventDefault();
        // data = new FormData($('#form')[0]);
        // let id = $(this).parent().attr('data-id');
        let state = '';
        // console.log();
        articleEdit(state, '修改成功');

    });
    function articleEdit(state,info) {
        $.ajax({
            type: 'post',
            url: BigNew.article_edit,
            data: {
                id,
                state,
                title: $('[name=title]').val().trim(),
                categoryId: $('[name=categoryId]').val(),
                cover: $('[name=cover]').val().trim(),
                date: $('[name=date]').val().trim(),
                content: editor2.txt.html()
            },
            success(backData) {
                if (backData.code == 200) {
                    alert(info);
                    window.location.href = './article_list.html';
                }
            }
        })
}
})